﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using IconEditorXBAP.IconElements;
using IconEditorXBAP.ModelicaElements;

namespace IconEditorXBAP
{
    public partial class Page1 : Page
    {
        private List<BasicShape> ListOfShapes = new List<BasicShape>();

        public Page1()
        {
            InitializeComponent();

            // po kliknuti by se melo provest rozparsovani obsahu okna InputTextBlock,
            // vytvorit kolekci BasicShapu a zavolat funkci, ktera je zobrazi na Canvas,
            // popripade nahlasit chybu syntaxe a nic nevykreslovat

            // changelog,
            // v 0.1, Martin, tato verze ma napevno zadratovane zobrazeni ikony Borata, nic neparsuje
            // v 0.1.1, Martin, prepsana hierarchie trid BasicShape
            // v 0.1.2, Martin, vytvorene extra tridy pro Modelicove recordy
            //                  svazani hodnot Modelicovych trid s grafickymi objekty pomoci Bindingu
            //                  prvni nastrel prace s extent u samotneho Icon objektu

        }

        private void ShowIcon_Click(object sender, RoutedEventArgs e)
        {
            // MessageBox.Show("This is only a demo, there's no parsing of input annotation,\nAlso, only 'extent' value is being modified, when loading annotation from Canvas", "Note",
            //     MessageBoxButton.OK, MessageBoxImage.Information);
            TopCanvas.Children.Clear();

            //foreach (BasicShape bs in TopCanvas.Children)
            //{
            //    TopCanvas.Children.Remove(bs);
            //}

                //String InputAnnotation = InputTextBlock.Text;
                //ListOfShapes = ParseAnnotation(InputAnnotation);
                // Parsing not yet implemented

            //{-100.0,-100.0,100.0,100.0,{Ellipse(true,{0,0,0},{255,127,127},LinePattern.Solid,FillPattern.Solid,0.25,{{-25.94,-24.44},{25.89,-3.273}}),Ellipse(true,{0,0,0},{255,127,127},LinePattern.Solid,FillPattern.Solid,0.25,{{-34.54,-10.05},{35.37,20.6}}),Ellipse(true,{0,0,0},{255,127,127},LinePattern.Solid,FillPattern.Solid,0.25,{{-49.95,8.29},{50.05,49.12}}),Text(true,{0,0,255},{0,0,255},LinePattern.Solid,FillPattern.Solid,0.25,{{-99.71,-65.65},{100.2,-25.6}},"%name",0.0,"")}}
            
            List<BasicShape> shapes = new List<BasicShape>();

            //Ellipse(true,{0,0,0},{255,127,127},LinePattern.Solid,FillPattern.Solid,0.25,{{-25.94,-24.44},{25.89,-3.273}}),
            ModelicaElements.Ellipse el1 = new ModelicaElements.Ellipse(
                true,
                Colors.Black,
                Color.FromRgb(255, 127, 127),
                LinePattern.Solid,
                FillPattern.None,
                0.25,
                new double[,] { { -25.94, -24.44 }, { 25.89, -3.273 } });

            //Ellipse(true,{0,0,0},{255,127,127},LinePattern.Solid,FillPattern.Solid,0.25,{{-34.54,-10.05},{35.37,20.6}}),
            ModelicaElements.Ellipse el2 = new ModelicaElements.Ellipse(
                true,
                Colors.Black,
                Color.FromRgb(255, 127, 127),
                LinePattern.Solid,
                FillPattern.None,
                0.25,
                new double[,] { { -34.54, -10.05 }, { 35.37, 20.6 } });

            //Ellipse(true,{0,0,0},{255,127,127},LinePattern.Solid,FillPattern.Solid,0.25,{{-49.95,8.29},{50.05,49.12}}),
            ModelicaElements.Ellipse el3 = new ModelicaElements.Ellipse(
               true,
               Colors.Black,
               Color.FromRgb(255, 127, 127),
               LinePattern.Solid,
               FillPattern.None,
               0.25,
               new double[,] { { -49.95, 8.29 }, { 50.05, 49.12 } });

            //Text(true,{0,0,255},{0,0,255},LinePattern.Solid,FillPattern.Solid,0.25,{{-99.71,-65.65},{100.2,-25.6}},"%name",0.0,"")}}
            List<TextStyle> l = new List<TextStyle>();
            l.Add(TextStyle.Bold);

            ModelicaElements.Text tx1 = new ModelicaElements.Text(
                true,
                Color.FromRgb(0, 0, 255),
                Color.FromRgb(0, 0, 255),
                LinePattern.Solid,
                FillPattern.Solid,
                0.25,
                new double[,] { { -99.71, -65.65 }, { 100.2, -25.6 } },
                "%name",
                12,
                "",
                l);

            shapes.Add(new MoEllipse(el1));
            shapes.Add(new MoEllipse(el2));
            shapes.Add(new MoEllipse(el3));
            shapes.Add(new MoText(tx1));

            foreach (BasicShape bs in shapes)
            {
                TopCanvas.Children.Add(bs);
            }

        }

        private void ShowIcon2_Click(object sender, RoutedEventArgs e)
        {
            TopCanvas.Children.Clear();

            //String InputAnnotation = InputTextBlock.Text;
            //ListOfShapes = ParseAnnotation(InputAnnotation);

            // Parsing not yet implemented

            List<BasicShape> shapes = new List<BasicShape>();

            //Ellipse(visible=true, fillColor={255,127,127}, fillPattern=FillPattern.Solid, extent={{56.05,-19.86},{106.9,0.8964}}),
            ModelicaElements.Ellipse el1 = new ModelicaElements.Ellipse(
                true,
                Colors.Black,
                Color.FromRgb(255, 127, 127),
                LinePattern.Solid,
                FillPattern.None,
                0.25,
                new double[,] { { 56.05, -19.86 }, { 106.9, 0.8964 } });

            //Ellipse(visible=true, fillColor={255,127,127}, fillPattern=FillPattern.Solid, extent={{-94.8,-15.43},{-24.89,15.22}}),
            ModelicaElements.Ellipse el2 = new ModelicaElements.Ellipse(
                true,
                Colors.Black,
                Color.FromRgb(255, 127, 127),
                LinePattern.Solid,
                FillPattern.None,
                0.25,
                new double[,] { { -94.8, -15.43 }, { -24.89, 15.22 } });

            //Ellipse(visible=true, fillColor={255,127,127}, fillPattern=FillPattern.Solid, extent={{-35.55,-18.83},{64.45,22}}),
            ModelicaElements.Ellipse el3 = new ModelicaElements.Ellipse(
               true,
               Colors.Black,
               Color.FromRgb(255, 127, 127),
               LinePattern.Solid,
               FillPattern.None,
               0.25,
               new double[,] { { -35.55, -18.83 }, { 64.45, 22 } });

            //Text(visible=true, lineColor={0,0,255}, fillColor={0,0,255}, fillPattern=FillPattern.Solid, extent={{-99.71,-84.51},{100.2,-44.46}},
            List<TextStyle> l = new List<TextStyle>();
            l.Add(TextStyle.Bold);

            ModelicaElements.Text tx1 = new ModelicaElements.Text(
                true,
                Color.FromRgb(0, 0, 255),
                Color.FromRgb(0, 0, 255),
                LinePattern.Solid,
                FillPattern.Solid,
                0.25,
                new double[,] { { -99.71, -84.51 }, { 100.2, -44.46 } },
                "%name",
                12,
                "",
                l);

            shapes.Add(new MoEllipse(el1));
            shapes.Add(new MoEllipse(el2));
            shapes.Add(new MoEllipse(el3));
            shapes.Add(new MoText(tx1));

            foreach (BasicShape bs in shapes)
            {
                TopCanvas.Children.Add(bs);
            }

        }

        private void ShowIcon3_Click(object sender, RoutedEventArgs e)
        {
            TopCanvas.Children.Clear();

            List<BasicShape> shapes = new List<BasicShape>();

            //{-100.0,-100.0,100.0,100.0,{
            
            //Line(true,{{-30.13,80.16},{-29.75,17.81}},{0,0,0},LinePattern.Solid,0.25,{Arrow.None,Arrow.None},3.0,false),            
            PointCollection ll = new PointCollection();
            ll.Add(new Point(-30.13,80.16));
            ll.Add(new Point(-29.75,17.81));

            ModelicaElements.Line li1 = new ModelicaElements.Line(
                true,
                ll,
                Colors.Black,
                LinePattern.Solid,
                2,
                new ModelicaElements.Arrow[] { ModelicaElements.Arrow.None, ModelicaElements.Arrow.None },
                3.0,
                false);

            //Rectangle(true,{0,0,0},{192,192,192},LinePattern.Solid,FillPattern.Solid,0.25,BorderPattern.None,{{-29.81,-47.62},{30.19,42.38}},0.0),
            ModelicaElements.Rectangle re1 = new ModelicaElements.Rectangle(
               true,
               Color.FromRgb(0,0,0),
               Color.FromRgb(192, 192, 192),
               LinePattern.Solid,
               FillPattern.Solid,
               0.25,
               BorderPattern.None,
               new double[,] { { -29.81, -47.62 }, { 30.19, 42.38 } },
               0.0);
            
            //Line(true,{{30.14,22.13},{30.14,82.13}},{0,0,0},LinePattern.Solid,0.25,{Arrow.None,Arrow.None},3.0,false),
            ll = new PointCollection();
            ll.Add(new Point(35.14, 22.13));
            ll.Add(new Point(30.14, 82.13));
            ll.Add(new Point(22.14, 50.13));
            ll.Add(new Point(10.14, 70.13));

            ModelicaElements.Line li2 = new ModelicaElements.Line(
                true,
                ll,
                Colors.Black,
                LinePattern.Solid,
                2,
                new ModelicaElements.Arrow[] { ModelicaElements.Arrow.None, ModelicaElements.Arrow.None },
                3.0,
                false);

            //Text(true,{0,0,0},{0,0,255},LinePattern.Solid,FillPattern.None,0.25,{{0.0,46.0},{0.0,106.0}},"%name",0.0,"")}}
            ModelicaElements.Text tx1 = new ModelicaElements.Text(
                true,
                Color.FromRgb(0, 0, 255),
                Color.FromRgb(0, 0, 255),
                LinePattern.Solid,
                FillPattern.Solid,
                0.25,
                new double[,] { { 0.0, 46.0 }, { 0.0, 106.0 } },
                "%name",
                12,
                "",
                new List<TextStyle>());

            //Polyline pl = new Polyline();
            //PointCollection pc = new PointCollection(ll);
            //pl.Points = pc; 
            //pl.Width = 100;
            //pl.Height = 120;
            //Canvas.SetLeft(pl, 10);
            //Canvas.SetTop(pl, 10);

            //pl.StrokeThickness = 3;
            //pl.Stroke = new SolidColorBrush(Colors.CornflowerBlue);
            //pl.Fill = new SolidColorBrush(Colors.Azure);

            //TopCanvas.Children.Add(pl);

            shapes.Add(new MoLine(li1));
            shapes.Add(new MoRectangle(re1));
            shapes.Add(new MoLine(li2));
            shapes.Add(new MoText(tx1));

            foreach (BasicShape bs in shapes)
            {
                TopCanvas.Children.Add(bs);
            }

        }

        //private List<BasicShape> ParseAnnotation(string InputAnnotation)
        //{
            

        //    return shapes;
        //}

        private void GetIcon_Click(object sender, RoutedEventArgs e)
        {
            String res = "";
            foreach (BasicShape bs in ListOfShapes)
            {
                res += bs.GetAnnotation();
            }

            OutputTextBox.Text = res;
        }
    }
}
